package com.leyou.order.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.item.client.ItemClient;
import com.leyou.item.entity.Sku;
import com.leyou.order.dto.CartDTO;
import com.leyou.order.dto.OrderStatusEnum;
import com.leyou.order.entity.Order;
import com.leyou.order.entity.OrderDetail;
import com.leyou.order.mapper.OrderDetailMapper;
import com.leyou.order.mapper.OrderMapper;
import com.leyou.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @PackageName: com.leyou.order.scheduled
 * @ClassName: OrderStatusscheduled
 * @Author: ruanboqi
 * @Date: 2020/8/27 0:10
 * @Description: TODO
 */

/**
 * 这是一个监控订单状态的定时任务
 */
@Slf4j
@Component
public class OrderStatusscheduled {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private ItemClient itemClient;

    //定时任务触发的方法
    @Scheduled(fixedDelay = 15000)//为了方便测试把1200000(20分钟)改成15秒(15000);
    public void autoQueryOrderStatus(){
        System.out.println("定时任务执行了"+new Date());
        //查询出所有订单状态为1的订单
        List<Order> orderList = orderService.queryPayOverTimeOrder();
        orderList.forEach(order -> {
            Date createTime = order.getCreateTime();
            Long time = createTime.getTime();
            //得到orderid
            Long id=order.getOrderId();
            //用户在超过20分钟不支付情况下
            if((System.currentTimeMillis()-time>= 30000)){//为了测试方便把1200000(20分钟),改成30s(30000)
                List<OrderDetail> orderDetails = orderService.queryOrderDetailsById(id);
                //定义一个map
                Map<Long, Integer> paramMap = orderDetails.stream().collect(Collectors.toMap(OrderDetail::getSkuId, OrderDetail::getNum));
                //远程调用增加库存
                itemClient.plusStock(paramMap);
                //修该订单的状态为7:支付过期
                System.out.println("定时任务的支付过期");
                orderService.updateOrderStatusByid(id,OrderStatusEnum.PAY_OVER_TIME.value());
            }
        });

            //查询出所有订单状态为7的订单
            List<Order> orderList1 = orderService.queryPayOverTimeOrder1();
            orderList1.forEach(order1 -> {
                Date createTime1 = order1.getCreateTime();
                Long time1=createTime1.getTime();
                Long id1=order1.getOrderId();
                //用户下单时间超过两小时,订单状态为失效
                if((System.currentTimeMillis()-time1>60000)){//为了测试方便,将1800000(2小时)改成60s(60000)
                    //修改订单状态为8:已失效
                    orderService.updateOrderStatusByid(id1,OrderStatusEnum.LOSE.value());
                    System.out.println("定时任务的订单失效");
                }
            });

    }



}


